var _API = {
	config : {
		domain : (function(){
			//var port = window.location.port || "80";
			var host = "http://sdc.oschina.mopaas.com";
			//return host;
			//if(port == "80") {
			//	return host;
			//}
			return "http://10.10.10.87:18080";
			//return "http://172.30.70.196:18080";
		})()
	}
};
_API.ajax = function(path, data, isHideLoading, fileData) {
	if(!data) {
		data = {};
	}
	data['_appType'] = "peopleId";
	data['...JSESSIONID'] = plus.storage.getItem("...JSESSIONID");
	
	var map = {
		success : function(){},
		error : function(){return false},
		other : {}
	};
	
	var error = function(json) {
		//针对自定义错误code
    	json.msgInfo = json.msg + ' (' + json.code + ")";
    	
    	//处理when
    	var temp = map.other[json.code];
    	if(temp) {
    		temp(json);
    		return ;
    	}
    	//回调Error
    	if(map.error(json)) {//返回true 表示已处理错误Code
    		return ;
    	}
    	
    	//通用异常Code处理
    	
	};
	if(path.indexOf("/")!=0) {
		path = "/" + path;
	}
	
	if(!isHideLoading) {
		plus.nativeUI.showWaiting();
	}
	
	if(fileData && fileData.length>0) {
		var task=plus.uploader.createUpload(_API.config.domain + path + ".json", {method:"POST"},
			function(t,status){ //上传完成
				if(!isHideLoading) {
					plus.nativeUI.closeWaiting();
				}
								
				if(status==200){
					var data = JSON.parse(t.responseText);
					if(data.code === 0){
						map.success(data);
		        	}else {
		        		error(data);
		        	}
				}else{
					error({code:-1000, msg:'上传失败，错误描述：' + status});
				}
			}
		);
		for(var i in data) {
			task.addData(i, data[i]);
		}
		for(var i=0; i<fileData.length; i++) {
			var f = fileData[i];
			task.addFile(f, {name:"file"});
		}
		task.start();
	} else {
		mui.ajax(_API.config.domain + path + ".json", {
			data: data,
			dataType:'json',//服务器返回json格式数据
			type:'post',//HTTP请求类型
			timeout:10000,//超时时间设置为10秒；
			success:function(data){
				if(!isHideLoading) {
					plus.nativeUI.closeWaiting();
				}
				
				if(data.code === 0){
					map.success(data);
	        	}else {
	        		error(data);
	        	}
			},
			error:function(xhr, type, errorThrown){
				if(!isHideLoading) {
					plus.nativeUI.closeWaiting();
				}
				error({code:-1000, msg:'错误描述：' + type});
			}
		});

	}
	
	return {
		success : function(back) {
			map.success = back;
			return this;
		},
		error : function(back) {
			map.error = back;
			return this;
		},
		when : function(errorCode, back) {
			map.other[errorCode] = back;
			return this;
		}
	};
};
_API.setCurrentCommunityId = function(id){
	localStorage.setItem("currentCommunityId", id);
};
_API.ajax2 = function(path, data, isHideLoading, files){
	if(!data) {
		data = {};
	}
	data.communityId = 'c844cfdf-ffe4-4219-a575-586b793e72de';//localStorage.getItem("currentCommunityId");
	if(!data.communityId) {
		//未登录...
	}
	return _API.ajax(path, data, isHideLoading, files);
};

//---------------
var fire = function(){
	var ids = ['view/me/index.html'];
	for(var i=0; i<ids.length; i++) {
		var detailPage = plus.webview.getWebviewById(ids[i]);
		if (detailPage) {
			mui.fire(detailPage, 'app:login');
		}
	}
}
_API.setPeople = function(people){
	if(people) {
		plus.storage.setItem("peopleInfo", JSON.stringify(people));
	}else {
		plus.storage.removeItem("peopleInfo");
	}
	fire();
}
_API.getPeople = function(){
	var json = plus.storage.getItem("peopleInfo");
	if(json){
		return JSON.parse(json);
	}
}

_API.checkLogin = function(){
	var param = {
		cellphone : plus.storage.getItem("cellphone"),
		password  : plus.storage.getItem("password"),
		getuiClientId: plus.push.getClientInfo().clientid,
		os: plus.os.name,
		osVersion: plus.os.version,
		appType: "PeopleApp",
		remember: "Y"
	}
	_API.ajax("account/login", param)
	.success(function(json){
		_API.setPeople(json['userInfo']);
	})
	.error(function(json){
		_API.setPeople();
		plus.nativeUI.toast("未登录帐号，功能受限");
	});
	_API.setPeople();
}
